package com.molichuxing.services.infrastructure.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.CarsViolationBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.CarsViolationMapper;
import com.molichuxing.services.infrastructure.dto.request.create.ViolationCreateDto;
import com.molichuxing.services.infrastructure.dto.response.CarViolationAppDto;
import com.molichuxing.services.infrastructure.dto.response.CarsViolationDto;
import com.molichuxing.services.infrastructure.dto.response.PieChartDto;
import com.molichuxing.services.infrastructure.dto.response.ViolationCountDto;
import com.molichuxing.services.infrastructure.service.CarViolationService;
import com.molichuxing.services.property.CarViolationEnum;
import com.molichuxing.services.property.CarViolationOpTypeEnum;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 16:17 2019/9/5
 * @Modified By:
 **/
@Service("carViolationService")
public class CarViolationServiceImpl implements CarViolationService {

    @Resource
    private CarsViolationMapper carsViolationMapper;

    @Override
    public Paged<CarsViolationDto> getCarsViolationPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        Paged<CarsViolationDto> paged = new Paged<>();
        paged.setTab(getTabList(map));
        if(null != map.get("startTime")){
            map.put("startTime", ObjectCastUtil.getDayStart(map.get("startTime")));
        }
        if(null != map.get("endTime")){
            map.put("endTime",ObjectCastUtil.getDayEnd(map.get("endTime")));
        }

        int count = carsViolationMapper.getCarsViolationPageCount(map);
        if(count <= 0) {
            return paged;
        }
        List<CarsViolationBean> result = carsViolationMapper.getCarsViolationPage(map, pageNum, pageSize);
        paged.setList(Convert.toCarsViolationDto(result));
        paged.setTotal(count);
        paged.setPageSize(pageSize);
        return paged;
    }

    @Override
    public List<CarsViolationDto> getCarsViolationByCarId(Integer carId) {
        List<CarsViolationBean> carsViolationList =  carsViolationMapper.getCarsViolationByCarId(carId);
        return Convert.toCarsViolationDto(carsViolationList);
    }

    @Override
    public List<CarsViolationDto> getCarsViolationByOrderCode(Long orderCode) {
        List<CarsViolationBean> carsViolationList =  carsViolationMapper.getCarsViolationByOrderCode(orderCode);
        return Convert.toCarsViolationDto(carsViolationList);
    }

    @Override
    public List<CarsViolationDto> getCarsViolationByUserId(Integer userId,Integer carId) {
        List<CarsViolationBean> carsViolationList =  carsViolationMapper.getCarsViolationByUserId(userId,carId);
        return Convert.toCarsViolationDto(carsViolationList);
    }

    /**
     * 违章待处理和待缴费记录的车辆id
     * @return
     */
    @Override
    public List<CarsViolationDto> getCarsViolationByStatus() {
        List<CarsViolationBean> carsViolationList =  carsViolationMapper.getCarsViolationByStatus();
        return Convert.toCarsViolationDto(carsViolationList);
    }


    /**
     * 违章待处理和待缴费记录的车辆id
     * @return
     */
    @Override
    public List<CarsViolationDto> getCarsViolationByVin() {
        List<CarsViolationBean> carsViolationList =  carsViolationMapper.getCarsViolationByVin();
        return Convert.toCarsViolationDto(carsViolationList);
    }

    @Override
    public Integer getViolationCountByUserId(Integer userId) {
        return carsViolationMapper.getViolationCountByUserId(userId);
    }

    @Override
    public List<CarViolationAppDto> getCarViolationAppDtoByUserId(Integer userId) {
        return carsViolationMapper.getCarViolationAppDtoByUserId(userId);
    }

    @Override
    public int createViolation(ViolationCreateDto violationCreateDto) {
        return carsViolationMapper.create(Convert.toCarsViolation(violationCreateDto));
    }


    @Override
    public int updateById(ViolationCreateDto violationCreateDto) {
        return carsViolationMapper.updateById(Convert.toCarsViolation(violationCreateDto));
    }

    @Override
    public CarsViolationDto getCarsViolationDto(Integer userId,Integer carId , LocalDateTime regulationTime) {
        CarsViolationBean carsViolationBean = carsViolationMapper.getCarsViolationDto(userId,carId,regulationTime);
        return Convert.toCarsViolationDto(carsViolationBean);
    }

    @Override
    public int modifyViolationStatusByCarId(Integer carId) {
        return carsViolationMapper.modifyViolationStatusByCarId(carId,CarViolationEnum.NOT_RENT.getValue());
    }

    /**
     * 违章统计
     * @return
     */
    @Override
    public ViolationCountDto getViolationStatisticsDto() {



        //总数量、总金额和总分数
        ViolationCountDto violationCountDto = carsViolationMapper.getSumStatistics();
        if(null == violationCountDto){
            return violationCountDto;
        }

        //数量
        List<PieChartDto> violationCountPieDto = carsViolationMapper.getViolationCountPieDto();
        if(null != violationCountPieDto && violationCountPieDto.size() > 0){
            violationCountDto.setViolationCountPieDto(violationCountPieDto);
        }

        //分数
        List<PieChartDto>  violationFractionPie = carsViolationMapper.getViolationFractionPie();
        if(null != violationFractionPie && violationFractionPie.size() > 0){
            violationCountDto.setViolationFractionPieDto(violationFractionPie);
        }

        //罚款金额
        List<PieChartDto>  violationFineFeePieDto = carsViolationMapper.getViolationFineFeePie();
        if(null != violationFineFeePieDto && violationFineFeePieDto.size() > 0){
            violationCountDto.setViolationFineFeePieDto(violationFineFeePieDto);
        }

        //最新统计时间
        violationCountDto.setStatisticsTime(DateUtil.getViolationStatisticsTimeStr(new Date()));

        return violationCountDto;
    }

    /**
     * 违章待处理统计
     * @return
     */
    @Override
    public Integer getViolationCount() {
        return carsViolationMapper.getViolationCount();
    }

    private List<TabDto> getTabList(Map<String, Object> map) {
        List<TabDto> tabList = new ArrayList<>();
        for(CarViolationEnum statusEnum : CarViolationEnum.values()) {
            int count = carsViolationMapper.getCarsViolationCount(map,statusEnum.getValue());
            TabDto tabDto = new TabDto();
            tabDto.setCount(count);
            tabDto.setName(statusEnum.getName());
            tabDto.setType(statusEnum.getValue());
            tabList.add(tabDto);
        }
        return tabList;
    }
    
    /**
     * 查询导出车辆违章数据
     * @param map
     *   ids(违章ids) licencePlate(车牌号) vin(车架号) userId(用户id)  dealerId(经销商id)  startDate（违章开始时间） endDate（违章结束时间）
     * @return
     */
    @Override
    public List<CarsViolationDto> getCarsViolationDownload(Map<String, Object> map) {
    	if(null != map.get("startTime")){
            map.put("startTime", ObjectCastUtil.getDayStart(map.get("startTime")));
        }
        if(null != map.get("endTime")){
            map.put("endTime",ObjectCastUtil.getDayEnd(map.get("endTime")));
        }

        List<CarsViolationBean> result = carsViolationMapper.getCarsViolationDownload(map);
    	return Convert.toCarsViolationDto(result);
    }

	/**
	 * 查询导出车辆违章数据条数
	 * 
	 * @param selectParams
	 * @return
	 */
	@Override
	public Long getCarsViolationDownloadCount(Map<String, Object> selectParams) {
		if (null != selectParams.get("startTime")) {
			selectParams.put("startTime", ObjectCastUtil.getDayStart(selectParams.get("startTime")));
		}
		if (null != selectParams.get("endTime")) {
			selectParams.put("endTime", ObjectCastUtil.getDayEnd(selectParams.get("endTime")));
		}

		return carsViolationMapper.getCarsViolationDownloadCount(selectParams);
	}

    @Override
    public Integer getViolationCountByCarId(Integer carId) {
       return carsViolationMapper.getViolationCountByCarId(carId);
    }

    /**
     *  根据id修改违章状态
     * @param id
     * @param type 1：标记已缴费 2：取消标记
     * @return
     */
    @Override
    public Boolean modifyStatusAndOpTypeById(Integer id, Integer type) {
    	Integer status= null;
    	Integer opType= null;
    	if(type==1) {
    		status=CarViolationEnum.NOT_RENT.getValue();
    		opType=CarViolationOpTypeEnum.MARK_PAID.getValue();
    	}else if(type==2) {
    		status=CarViolationEnum.FOR_RENT.getValue();
    		opType=CarViolationOpTypeEnum.UNMARK.getValue();
    	}else {
    		return false;
    	}
    	int num = carsViolationMapper.modifyStatusAndOpTypeById(id,status,opType);		
    	return num == 1;
    }
}
